package com.example.harmonyosproject.server.service.impl;
import com.example.harmonyosproject.pojo.PasswordChangeRequest;
import com.example.harmonyosproject.pojo.User;
import com.example.harmonyosproject.server.mapper.UserMapper;
import com.example.harmonyosproject.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean register(User user){
        User user1=userMapper.getByPhone(user.getPhone());
        if(user1!=null)
            return false;
        user.setUsername(user.getName());
        userMapper.addUser(user);
        return true;
    }

    @Override
    public User getByPhone(String phone){
        User user = userMapper.getByPhone(phone);
        //log.info(user.toString());
        return user;
    }

    @Override
    public User getById(Integer id) {
        User user=userMapper.getById(id);
        return user;
    }

    @Override
    public User login(User user) {
        User user1=userMapper.login(user);
        return user1;
    }

    @Override
    public void changeInfo(User user) {
        userMapper.updataInfo(user);
    }

    @Override
    public Boolean changePassword(PasswordChangeRequest passwordChangeRequest) {
        User user=userMapper.getById(passwordChangeRequest.getId());
        if (!passwordChangeRequest.getOldPassword().contentEquals(user.getPassword())){
            return false;
        }
        else {
            log.info(passwordChangeRequest.getNewPassword());
            user.setPassword(passwordChangeRequest.getNewPassword());
            userMapper.updatePassword(user);
            return true;
        }
    }

}
